package yunplus.io.hchome.model.api;

import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import yunplus.io.hchome.model.api.contract.IFPMCallback;
import yunplus.io.hchome.model.api.contract.IInput;
import yunplus.io.hchome.model.api.contract.IWS;
import yunplus.io.hchome.model.api.contract.IWSInput;
import yunplus.io.hchome.model.api.implement.FPMInput;
import yunplus.io.hchome.model.api.implement.FPMWS;
import yunplus.io.hchome.model.api.implement.FPMWSInput;

import static android.R.attr.value;

/**
 * Created by WangFan on 2016/12/29.
 */

public class FPM{

    private final static String TAG = "FPM";

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient _client;

    private String _host;

    private final String _pingURL = "/ping";

    private FPM(){
        _client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(1, TimeUnit.MINUTES)
                .build();
    }

    private static FPM _me = new FPM();

    public static FPM me(){
        return _me;
    }

    public FPM init(String host){
        this._host = host;
        return this;
    }

    public void ping(final IFPMCallback callback){
        //TODO ping
        callback.onSuccess(null);
    }

//    private Call post(JSONObject value) {
//        RequestBody formBody = RequestBody.create(JSON, value.toJSONString()); //formBuilder.build();
//        Request request = new Request.Builder().url(_host).post(formBody).build();
//        Call call = _client.newCall(request);
//        return call;
//    }

    private Call post(IInput value) {
        RequestBody formBody = RequestBody.create(JSON, value.toJson()); //formBuilder.build();
        Request request = new Request.Builder().url(_host).post(formBody).build();
        Call call = _client.newCall(request);
        return call;
    }

    public void call(@NonNull int code, @NonNull String user, JSONObject args, final IFPMCallback callback){
        IInput input = new FPMInput(code, user, args);
        Log.d(TAG, input.toJson());
        post(input).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                callback.onFailure("-999", e.getLocalizedMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String body = response.body().string();
                JSONObject result = JSONObject.parseObject(body);
                Integer status = result.getInteger("status");
                if(status == 0){
                    callback.onSuccess(result);
                }else{
                    callback.onFailure("" + status, result.toJSONString());
                }

            }
        });

    }




}
